function add5(x: number, y?: number){
  return y? x + y : x
}

function add6(x: number, y = 0, z: number, q = 1) {
  return x + y + z + q
}

// console.log(add6(1,undefined,2));

function add7(x: number, ...rest: number[]) {
  return x + rest.reduce((prev, cur) => prev + cur)
}

// function add8(...rest: number[]): number
// function add8(...rest: string[]): string
function add8(...rest: any[]): any {
  let first = rest[0]
  if(typeof first === 'string') {
    return rest.join('')
  }
  if(typeof first === 'number') {
    return rest.reduce((prev, cur) => prev + cur)
  }
}
console.log(add8(1,2,3));
console.log(add8('1',2,3));

